package cn.stylefeng.guns.utils;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.CertAlipayRequest;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.internal.util.AlipayEncrypt;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class AliUtil {
    public static class AliConfig{
        // TODO: 2024/10/31 此处应该配置黄蚂蚁的配置数据 
        //测试环境
        //public static String api = "https://openapi.alipay.com/gateway.do";
        //public static String appId = "2021004187662343";
        //public static String merchantPrivateKey ="MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDl3umk/Jd9gISA3LnvAFAboDkElHdxf1hDYK/4fCBWhlfRhx199gaYzs8oLw2HtSuaek3xSbnIf15gkIHv7EWQ3dZS9AS9JsK1wKYLKbhq8qwWo357Kzl5cYbbVzFkEEd2UE8NlRa9WyKYD/+UaoSjdB4nbm1U47TW+snG4GF2OLpagty7H8w1X/8lhArH5bBdyvzJaMSmv2GQrFMGggzXoE68PMJTKf568qW9UzqiOF2PXn2hNZQGBuUafLgdIw9qL5/+dw9gUm0HfoS5g4z3HT7/4yI5diYwXjgkp8RFKl2Xfau4QovT+V0zujT/VqdAI2IFLGI2vvuys1373g6pAgMBAAECggEADtRmtmIlU89qvPWAeU7H3EBl7Um2uvfANfiS4bEAVRStrbLN0EaC/nC5JJqEgPDLM1gYlyxuc+Tjrc8k0tmnU0U8gnWDqMBLZ3ZaP/YdE6k8OOmrSz7qok8UqCnj4dS7B4L+1p17rhOnE7BUiLqme/UNzPVu0D8wd34ObmojX4ECDKmBjakvHfYjuJeVxbNf8k+rRF41ejLA0b9FsnoM1h8dKtVczE0KX8S6FYGuwMP3rJqH8mSJwtz0MUnPNgsMMEUhUZOF7YMzBdeXdrqrRKwIymVKsfmv7H0nmCGy1J6aOnZ4FNndLo1St6hq5iqDH8hHei5MeuEcfeb+YYzFUQKBgQD/xUC0j7orQ7ROExV3H45LcvecQ9g8xke0c/mcfIrFpovxyDsbx3JaU+OtKQ4Jm4MTivaEGSG50ESsAFo0D2a3mMRHDbZZ+V/NCvbwhBxbncShRnFhpALh2phT956mTEyQVm4PVq/aKq3u4Hn5Y6A/TTet476pYOmQ67tDIdjLBwKBgQDmE7YIvBBUOFDESD3AoMOOOwP2cCl8eBFJiiomFbpJXG94sAnoUnKyJqDnAYOpfbQNMRQkWBf120Fuu8y/pHyCulS6I5AOtZfE+q0KQlo8SaVNpydVF/hb8DphzBRSJVfwENJsd7y2euI906qkznztgGYOkAEOvj6RWI83o9H8zwKBgAq0f9PO3DTpXYeTBuf4eyUvR6vTrx5EEkY8QGtLTyeco+jXv5mXDHM5ZaMev8apKqBLxaubvaygSe+FCEF7j4oJq9VpfoNfi6HyXomMjf9LqCoz35T9n11Cn/OLvqkpMl8sLukXcL7J4IPKA3DruY5MthLiIF3If54gL7NMnF1LAoGBANYNCJJfxSFVs26pdyo7ZzRwG2OvPima2xp7/HKqTEHsSZkSzmTiL+smnRPOCNwrItQTERYvDzug3ONUvUyej2hgSmtSorfMooKUZWDiEml8atov5Rv5zQpcGTTbVRyHuaSTQfHABEYRVWMIBf6GTRv5FPn/Z7x7qmI27idptfdpAoGBAJOGpNRcj8YLwpxdiJtyCEH2cC22Jwjn8LUn8OpIrgiao9I7R7MzkFivUuC01tpGfFA6eS0heRxNgT2W0fP2tkpwyw7+GondmyuwdutkXFLVy/MWfrIAd3OJVu+WF5sROOyviWWCYsLdVuf1QKV0DMlrsePN0Z+WW1PKWVdasCoe";
        //public static String merchantCertPath = "D:\\BaiduNetdiskDownload\\cert\\new1\\appCertPublicKey_2021004187662343.crt";
        //public static String alipayCertPath = "D:\\BaiduNetdiskDownload\\cert\\new1\\alipayCertPublicKey_RSA2.crt";
        //public static String alipayRootCertPath = "D:\\BaiduNetdiskDownload\\cert\\new1\\alipayRootCert.crt";

        //正式环境
//        public static String api = "https://openapi.alipay.com/gateway.do";
//        public static String appId = "2021002121603444";
//        public static String merchantPrivateKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCTARJ2bMFJo5p5n8EWJaesmPzBEaIMkl6hVBFVwE0SFoClUrB48WC4iRF3PnbEfNxav0B7dGh/SK64GmqCz3SfUspWGmq0cDM+JJ/HQoiY/d2aLwayovzWv5S9B96c4oeFmDCbZaFej3GZFgbeSdhpw09C7XG6Gl1d3miqTwJm4vSghNUWknabAMa2PqtSgOA9IkW6v91qArp8inJX5r5qX2v73EOQVONbu/rc2GW61pu5NUfFFNAIfg19ab8AAZBAPnVjI8S1NzahixHlfEju9vvJk0qVGl+bdYOYcvdMdUCa0fAYHNHz4QG43rCZOvitF3WnY/tIhFtQ6zWFc+8hAgMBAAECggEABRbrabg4NPF6Hq3eRIMHPB2L2q9emerTZeiANSTURkULLS4gt/87Zwgv154NBJ6CDo5CkEOmfomoqu0XNPIzynRWVwbRHPAphgREl+SA0DMx4e2w4Kxt7o1xe93y4iGhqYlsXWT2Yky7FR7DHB0YZgkCDIImBth01RQnMYZMwf0Z7B4Bdk/AP3on54d21G9mYxUqS/vpeemWFe3s4YYrSV6pRxvaZfUVU7idALMCtgra2khI33LwQ3ukpm3/ZWgOr4+P9Ona6zxXA3NgluAa8zKsbQOviG2pk47ZRyhdlTgmBd2euGquF8HheKquLpeUQxCr06Dw5W9n+Owa5c23wQKBgQDpGkV5DZAGkARLpJJjK8M7CXNgrfV+Qb8Nq3XwqELF8NJFGePUhWhWcO9oUGwhz75t9Td68BzaOxngWtujvvHKBmkBAQccFd0XWObvohjOipDFaetTnbHXZKCJr96zwf3M5i5PACOayldYuiA4hp67tQTY16oLlHxTsJpDf2nE2QKBgQChcbixojOt2GUjqkid6OMCrexXcA2QQYe1cjFpQ+MPp+2oNMTtp1umqdGRYpA1zymtsZXe6fmvWhg65DPduAp2DObFc2U5XeJjAJ1cHSzj9lMTGDmsQ24sPKafpdOzZbdImtewNUNItq9RYY/GyzgQ5QNH0gJPj+SraT+0F8vviQKBgQCrQ5EdkrhqCfAN/5lKYcHsI1JxCv7ex8FucraRnZ6JvhlPiekZoPb647kSZFw+L1tTgKJFohsgv+GQNFFBqq4NZ85IXY/ys6GgYMIz+F3PioE5ekvbQHaEsbs9zkiKvBIUNF57zrdaKCYq232G6mm/sg0goLugDHWYK537z6ZPaQKBgEtt8rkr67Fh1X9gZCVDVi0+TZpcxDmOJHXmMKfaDBCYiTV50jinZO7tkPi4cGBztiltO3S3JO2EzqVVUdZZ0qltRbUjQTwYXVmcQ+lxyDOKNy5It0mBB8yrWb5loA/Fimjlz7iIajNjB26igqaC9GMQXQQFXwcFV/WuaxEdvS7hAoGALhMDikVy47q9dvX9iSeipmjc3+cgMfYBCmIpF0AIMzTEg4DOyZEHGL8wxum2dxPaI7f5TSIOkwHFaaLZn0RvhE6nAwCPlkeMBOQvJWJXr43NQjLhp1Ksia/aRCRgf5SaGu7z27VMFgpf08yZyghsERVtrgcAD6Hx24O3v++LCxw=";
//        public static String merchantCertPath = "D:/flowSys/cert/appCertPublicKey_2021002121603444.crt";
//        public static String alipayCertPath = "D:/flowSys/cert/alipayCertPublicKey_RSA2.crt";
//        public static String alipayRootCertPath = "D:/flowSys/cert/alipayRootCert.crt";

        //linux正式环境
        public static String api = "https://openapi.alipay.com/gateway.do";
        public static String appId = "2021004187662343";
        public static String merchantPrivateKey ="MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDl3umk/Jd9gISA3LnvAFAboDkElHdxf1hDYK/4fCBWhlfRhx199gaYzs8oLw2HtSuaek3xSbnIf15gkIHv7EWQ3dZS9AS9JsK1wKYLKbhq8qwWo357Kzl5cYbbVzFkEEd2UE8NlRa9WyKYD/+UaoSjdB4nbm1U47TW+snG4GF2OLpagty7H8w1X/8lhArH5bBdyvzJaMSmv2GQrFMGggzXoE68PMJTKf568qW9UzqiOF2PXn2hNZQGBuUafLgdIw9qL5/+dw9gUm0HfoS5g4z3HT7/4yI5diY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n/Z7x7qmI27idptfdpAoGBAJOGpNRcj8YLwpxdiJtyCEH2cC22Jwjn8LUn8OpIrgiao9I7R7MzkFivUuC01tpGfFA6eS0heRxNgT2W0fP2tkpwyw7+GondmyuwdutkXFLVy/MWfrIAd3OJVu+WF5sROOyviWWCYsLdVuf1QKV0DMlrsePN0Z+WW1PKWVdasCoe";
        public static String merchantCertPath = "/usr/local/flow/cert/appCertPublicKey_2021004187662343.crt";
        public static String alipayCertPath = "/usr/local/flow/cert/alipayCertPublicKey_RSA2.crt";
        public static String alipayRootCertPath = "/usr/local/flow/cert/alipayRootCert.crt";
        //解密的key

        //解密
        static String charset = "UTF-8"; // 字符集
        static String encryptType = "AES"; // 加密类型，固定值
        static String decryptKey = "6QmizNEYhqGakuMltPHdIg==";// AES 密钥，控制台配置的接口内容加密方式的值
        public static String AESDecrypt(String encryptedData) throws AlipayApiException {
            String plainData = AlipayEncrypt.decryptContent(encryptedData, encryptType, decryptKey,charset);
            return plainData;
        }
    }

    public static class AlipayConfig{
        //字符
        public static String CHARSET = "UTF-8";

        public static String SIGNTYPE = "RSA2";
        //public static String notifyUrl = "https://asdf.yumker.com/pay/notifyAliPay";
        //public static String notifyUrl = "https://www.yellowant.net/pay/notifyAliPay";
        // TODO: 2024/10/29 notify
        //public static String notifyUrl = "https://2lz0280rv942.vicp.fun/pay/notifyAliPay";
        public static String notifyUrl = "https://www.hbbbj.cn/pay/notifyAliPay";
    }
    AlipayClient alipayClient;
    //获取用户资料
    public AliUtil() throws AlipayApiException {
        alipayClient = new DefaultAlipayClient(getClientParams());
    }

    public AlipayClient getAlipayClient(){
        return alipayClient;
    }

    private static CertAlipayRequest getClientParams() {
        CertAlipayRequest certParams = new CertAlipayRequest();
        certParams.setServerUrl(AliConfig.api);
        //请更换为您的AppId
        certParams.setAppId(AliConfig.appId);
        //请更换为您的PKCS8格式的应用私钥
        certParams.setPrivateKey(AliConfig.merchantPrivateKey);
        //请更换为您使用的字符集编码，推荐采用utf-8
        certParams.setCharset("utf-8");
        certParams.setFormat("json");
        certParams.setSignType("RSA2");
        //请更换为您的应用公钥证书文件路径
        certParams.setCertPath(AliConfig.merchantCertPath);
        //请更换您的支付宝公钥证书文件路径
        certParams.setAlipayPublicCertPath(AliConfig.alipayCertPath);
        //更换为支付宝根证书文件路径
        certParams.setRootCertPath(AliConfig.alipayRootCertPath);
        return certParams;
    }

    /**
     * 将request中的参数转换成Map
     *
     * @param request
     * @return
     */
    public static Map<String, String> convertRequestParamsToMap(HttpServletRequest request) {
        Map<String, String> retMap = new HashMap<String, String>();
        Set<Map.Entry<String, String[]>> entrySet = request.getParameterMap().entrySet();
        for (Map.Entry<String, String[]> entry : entrySet) {
            String name = entry.getKey();
            String[] values = entry.getValue();
            int valLen = values.length;
            if (valLen == 1) {
                retMap.put(name, values[0]);
            } else if (valLen > 1) {
                StringBuilder sb = new StringBuilder();
                for (String val : values) {
                    sb.append(",").append(val);
                }
                retMap.put(name, sb.toString().substring(1));
            } else {
                retMap.put(name, "");
            }
        }
        return retMap;
    }

    /**
     * 把map转为对象
     *
     * @param params
     * @return
     */
    public static AlipayNotifyParam buildAlipayNotifyParam(Map<String, String> params) {
        String json = JSON.toJSONString(params);
        return JSON.parseObject(json, AlipayNotifyParam.class);
    }

}
